package com.baomidou.springwind.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.Project;
import com.baomidou.springwind.entity.PubFile;
import com.baomidou.springwind.service.FileService;
import com.baomidou.springwind.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文件管理相关操作
 * </p>
 *
 * @Author demo
 * @Date 2016/4/15 15:03
 */
@Controller
@RequestMapping("/sys/project")
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Permission("1001")
    @RequestMapping("/list")
    public String list(Model model) {
        return pjaxPath("/project/list", model);
    }

    @Permission("1001")
    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        if (id != null) {
            model.addAttribute("project", projectService.selectById(id));
        }
        return "/project/edit";
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/getProjectList")
    public String getProjectList(Page<Project> page) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        return jsonPage(projectService.selectPage(page, wrapper));
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/save")
    public String save(Project project) {
        if (project == null) {
            callbackFail("错误请求参数");
        }
        if (project.getId() != null) {
            project.setModifyTime(new Date());
            projectService.updateById(project);
        } else {
            project.setOperatorId(getCurrentUserId());
            project.setCreateTime(new Date());
            projectService.insert(project);
        }
        return callbackSuccess(true);
    }
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/save1")
    public String save1() {
        //保存项目
        Project project=new Project();
        project.setName(request.getParameter("project"));
        project.setState(Integer.valueOf(request.getParameter("type")));
        project.setContent("请在项目维护里面添加描述");
        project.setOperatorId(getCurrentUserId());
        project.setCreateTime(new Date());
        projectService.insert(project);
        //查询最新项目
        JSONObject ret = new JSONObject();
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        List<Project> projects =projectService.selectList(wrapper);
        ret.put("prj",projects.get(0));
        return callbackSuccess(ret);
    }

    /**
     * 城市数据导入--
     */
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/project/query")
    public String queryProjectList(Model model) {
        return callbackSuccess(projectService.getUserAllProjects());
    }
}
